<?php

namespace Cblood\Route\Http;


class HttpResponse extends Http
{
    const RESPOND_HTML = 'text/html';
    const RESPOND_JSON = 'application/json';
    const RESPOND_XML = 'text/xml';
    const RESPOND_IMAGE = 'application/octet-stream';
    const RESPOND_DOWNLOAD = 'image/gif,image/png,image/jpg,image/jpeg';

    protected $responseLanguage = 'zh-cn';
    protected $responseCode = '200';
    protected $responseCharset = 'utf-8';
    protected $respondType = self::RESPOND_HTML;

    public function skip($url)
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_SKIP,
            ['url' => $url]);
//        header('location:' . $url);
    }

    public function image($filePath)
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_IMAGE,
            ['filePath' => $filePath]);
    }

    public function download($filePath)
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_DOWNLOAD,
            ['filePath' => $filePath]);
    }

    public function view($template, $params = [])
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_VIEW,
            ['template' => $template, 'params' => $params]);
    }

    public function xml($params = [])
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_XML,
            ['params' => $params]);
    }

    public function json($params = [])
    {
        return new HttpResponseResult(HttpResponseResult::RESPONSE_JSON,
            ['params' => $params]);
    }

    public function setCharset($charset = 'utf-8')
    {
        $this->responseCharset = $charset;
    }

    /**
     * @param string $respondType 可选参数 ： RESPOND_HTML，RESPOND_JSON，RESPOND_XML
     */
    public function setRespondType($respondType = self::RESPOND_HTML)
    {
        $this->respondType = $respondType;
    }

    /**
     * 新增 Content-Type
     * @param string $name
     */
    public function addRespondType($name)
    {
        header('Content-Type:' . $name);
    }

    /**
     * 设置响应码
     * @param int $status
     */
    public function setResponseCode($status = 200)
    {
        $this->responseCode = $status;
    }

    public function setLanguage($language = 'zh-cn')
    {
        $this->responseLanguage = $language;
    }

    public function setDefaultResponse()
    {
        http_response_code($this->responseCode);
        header('Content-language: ' . $this->responseLanguage);
        header('Content-Type:charset=' . $this->responseCharset);
        header('Content-Type:' . $this->respondType);
    }
}